const express = require('express');//引入
const Router = express.Router();

let { find } = require('../db/mongodb');

function toast(opt) {
    let defaults = {  //默认参数
        code: 1,
        mes: '操作成功',
        data: []
    }
    if (opt) {   //替补参数
        Object.assign(defaults, opt);
        if (opt.code == 0) {
            defaults.mes = '操作失败'
        }
    }
    return defaults;
}


Router.route('/watch')//查询
    .get(async (req, res) => {
        let { shopName } = req.query;
        let result = await find('Product', { shopName });
        res.send(result);
    });
////////////////////////////////////后台请求数据
Router.route('/hFind')
    .get(async (req, res) => {//查询
        let data2 = [];
        let result = await find('Product');
        if (result.length >= 1) {
            data2 = result;
            res.send(toast({ data: data2 }));
        }
    });
//导出
module.exports = Router;